<template>
	<view class="container">
		<u-navbar :is-fixed="false" :border-bottom="false" :is-back="true" back-icon-name="arrow-leftward" title="个人资料"
			back-icon-size="35" :background="{ background: '#fff' }" title-color="#000">
			
		</u-navbar>

		<view class="content">
			<!-- <view class="avatar">
				<image :src="userInfo.avatar" class="avatar-image" mode=""></image>
				<view class="a" @click="handleChooseImg">更换头像</view>
			</view> -->
			<view class="form">
				<view class="cell">
					<view class="name"><span style="color: #f33;">*</span>昵称</view>
					<input type="text" placeholder="请输入" v-model="detail.userName" class="ipt"
						placeholder-class="hold" />
				</view>
				<!-- <view class="cell">
					<view class="name">生日</view>
					<picker mode="date" class="ipt" :class="{ hold: !detail.birthday }"
						@change="changeDefaultPicker('birthday', $event)">
						<view>{{ detail.birthday || '请选择生日' }}</view>
					</picker>
				</view> -->
				<!-- <view class="cell">
					<view class="name">性别</view>
					<picker mode="selector" :range="sexPicker" range-key="name" class="ipt"
						:class="{ hold: !detail.sex }" @change="changePicker('sex', $event)">
						<view>{{ detail.sex === 1?'男':'女' || '请选择性别' }}</view>
					</picker>
				</view> -->
				<view class="cell">
					<view class="name">手机号</view>
					<viwe class="ipt">
						{{ detail.account || '--' }}
					</viwe>
					
				</view>
				<view class="cell">
					<view class="name">邮箱</view>
					<input placeholder="请输入" v-model="detail.email" class="ipt" placeholder-class="hold" />
				</view>
				<!--<view class="cell">
					<view class="name">国家/地区</view>
					<pickRegions :defaultRegion="detail.region" v-model="detail.region"
						:class="{ hold: !detail.region, ipt: true }">
						<view>{{ detail.region || '请选择国家/地区' }}</view>
					</pickRegions>
				</view>-->
			</view>
		</view>
		<!--  -->
		<view class="back" @click="submit">保存用户信息</view>
		<!-- <view class="back" @click="logout">退出登录</view> -->
	</view>
</template>

<script>
	import pickRegions from '@/components/pick-regions/pick-regions.vue';
	export default {
		components: {
			pickRegions
		},
		data() {
			return {
				sexPicker: [{
						name: '男'
					},
					{
						name: '女'
					}
				],
				detail: {},
				form:{
					"avatar": "",
					"name": "",
					"realName": "",
					"phone": "",
					"email": "",
					"id": ""
				}
			};
		},
		onLoad() {
			this.detail=uni.getStorageSync('userInfo')
			console.log(this.detail)
			// this.$u.api.userInfo().then(data => {
			// 	console.log(data);
			// 	if (data.success) {
			// 		this.detail = data.data;
			// 	}
			// }).catch(err => {
			// 	console.log(err)
			// 	this.$u.func.showToast({
			// 		title: err,
			// 	})
			// })
		},
		methods: {
			logout() {
				this.$u.func.logout();
			},
			submit() {
				let that=this
				if(!this.detail.userName){
					uni.showToast({
						icon:'none',
						title: '请输入用户昵称',
						duration: 2000
					});
					return false
				}
				this.form.name=this.detail.userName
				this.form.realName=this.detail.userName
				this.form.phone=this.detail.account
				this.form.email=this.detail.email
				this.form.id=this.detail.userId
				uni.showLoading({title: '正在保存...'})
				this.$u.api.updateUser(this.form).then(data => {
					uni.hideLoading()
					if(data.code){
						uni.showToast({
							icon:'none',
							title: '保存成功',
							duration: 2000
						});
						uni.setStorageSync('userInfo',this.detail)
						uni.navigateBack({ delta: 1 });
					}
				})
				// console.log(JSON.stringify(this.form));
			},
			changeDefaultPicker(name, e) {
				this.detail[name] = e.detail.value;
			},
			changePicker(name, e) {
				this.detail[name] = this[`${name}Picker`][e.detail.value].name;
			},
			handleChooseImg() {
				let that = this;
				uni.chooseImage({
					count: 1,
					success(e) {
						that.detail.avatar = e.tempFilePaths[0];
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.container {
		background-color: #f7f7f7;
		min-height: 100vh;
		padding: 0 0 200rpx;
		overflow: hidden;
	}

	.complie {
		vertical-align: middle;
		padding: 10rpx 20rpx;
		font-size: 32rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #03A9F4;
	}

	.content {
		background-color: #ffffff;
		overflow: hidden;
	}

	.avatar {
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;

		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #03A9F4;
		.avatar-image {
			width: 162rpx;
			height: 162rpx;
			background: #ffffff;
			border-radius: 50%;
			background-color: #82848a;
			margin-bottom: 15rpx;
		}
	}

	.form {
		padding: 0 36rpx;

		.cell {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;

			font-size: 30rpx;
			font-family: Source Han Sans CN;
			font-weight: bold;
			color: #666666;

			&:not(:last-of-type) {
				border-bottom: 2rpx solid #efefef;
			}

			.ipt {
				flex: 1;
				margin-left: 40rpx;
				text-align: right;
				font-size: 28rpx;
				color: #333333;
				line-height: 80rpx;
				height: 80rpx;
			}

			.hold {
				font-size: 27rpx;
				font-family: Source Han Sans CN;
				font-weight: bold;
				color: #c8c8ce;
			}
		}
	}

	.back {
		margin-top: 40rpx;
		text-align: center;
		height: 109rpx;
		background: #ffffff;

		font-size: 34rpx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: #141414;
		line-height: 109rpx;
	}
</style>
